package com.free.files.fdfs;

import com.free.common.vo.ResultModel;
import com.free.exception.SystemException;
import com.free.files.IFiler;
import com.free.utils.FreeFileUtils;
import org.apache.commons.lang3.StringUtils;
import org.csource.fastdfs.StorageClient1;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
public class FdfsFiler implements IFiler {

    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/

    private FastdfsConfiguer configuer;

    private static final Map<String,IFiler> cache = new HashMap<>();

    private static Object obj = new Object();
    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    private FdfsFiler(){

    }

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/
    public static IFiler instance(String conf) throws SystemException {
        synchronized (obj){
            if(!cache.containsKey(conf)){
                synchronized (obj){
                    FastdfsConfiguer config = null;
                    if(StringUtils.isEmpty(conf)){
                        config = FastdfsConfiguer.instance();
                    }else{
                        config = FastdfsConfiguer.instance(conf);
                    }
                    FdfsFiler filer = new FdfsFiler();
                    filer.setConfiguer(config);
                    cache.put(conf, filer);
                }
            }
        }
        return cache.get(conf);
    }

    @Override
    public ResultModel upload(byte[] content, String serverPath) throws SystemException {
        StorageClient1 storage = null;
        try {
            storage = configuer.getStorageClient1();
        } catch (SystemException e) {
            throw e;
        }
        try {
           String res = storage.upload_file1(content, serverPath, null);
           return ResultModel.success(res);
        } catch (Exception e) {
           throw new SystemException("FastDfs上传文件失败", e);
        }
    }

    /**
     *
     * @param localPath 本地路径
     * @param groupName GroupName
     * @return
     * @throws SystemException
     */
    @Override
    public ResultModel upload(String localPath, String groupName) throws SystemException {
        StorageClient1 storage = null;
        try {
            storage = configuer.getStorageClient1();
        } catch (SystemException e) {
            throw e;
        }
        String ext = FreeFileUtils.getFileExt(localPath);
        try {
           String serverLink = storage.upload_file1(groupName, localPath,ext,null);
            return ResultModel.success(serverLink);
        } catch (Exception e) {
            throw new SystemException("FastDfs上传文件失败", e);
        }
    }

    @Override
    public ResultModel downLoad(String serverPath) throws SystemException {
        StorageClient1 storage = null;
        try {
            storage = configuer.getStorageClient1();
        } catch (SystemException e) {
            throw e;
        }
        try {
            return ResultModel.success(storage.download_file1(serverPath));
        } catch (Exception e) {
            throw new SystemException("FastDfs下载文件失败", e);
        }
    }

    @Override
    public ResultModel downLoad(String serverPath, String localPath) throws SystemException {
        StorageClient1 storage = null;
        try {
            storage = configuer.getStorageClient1();
        } catch (SystemException e) {
            throw e;
        }
        try {
            int res = storage.download_file1(serverPath,localPath);
            return ResultModel.success(res);
        } catch (Exception e) {
            throw new SystemException("FastDfs下载文件失败", e);
        }
    }

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
   private void setConfiguer(FastdfsConfiguer configuer){
        this.configuer = configuer;
   }

}
